const redisLib = require('redis');
const logUtil = require('./log_util');
const { redis } = require('../config');

const client = redisLib.createClient(redis);

client.on('error', (err) => {
  logUtil.error('connect to redis error', err);
  process.exit();
});

client.on('connect', (connect) => {
  logUtil.info('redis connect success', connect);
});

module.exports = {
  getFromRedis: key => new Promise((res, rej) => {
    client.get(key, (err, reply) => {
      if (err) {
        rej(err);
      } else {
        res(reply);
      }
    });
  }),
  setToRedis: (key, value, timeOut = 60 * 60 * 24, timeType = 'EX') => {
    client.set(key, value, timeType, timeOut);
  }
};
